import Vue from 'vue'
import VueRouter from 'vue-router'
import Home2 from '../components/Home2.vue'
import Login2 from '../components/Login2.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login2
  },
  {
    path: '/comps',
    component: () => import(/* webpackChunkName: "about" */ '../components/component/Comp2.vue')
  },
  {
    path: '/order',
    component: () => import(/* webpackChunkName: "about" */ '../components/order/Order2.vue')
  },
  {
    path: '/contacts',
    component: () => import(/* webpackChunkName: "about" */ '../components/contacts/Contacts2.vue')
  },
  {
    path: '/supplier',
    component: () => import(/* webpackChunkName: "about" */ '../components/supplier/Supplier2.vue')
  },
  {
    path: '/home',
    component: Home2
  }
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转

  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('username')
  // if (!tokenStr) return next('/login')
  next()
})
export default router
